<?php

class SystemAction extends BaseAction {

	public function _initialize(){
		parent::_initialize();

	}

    public function site(){
    	$data = D('Setting')->getset();
    	$this->assign('data',$data);
    	$this->show();
    }

    public function banner(){

        $map['key'] = 'bannernum';

        $num  = D('Setting')->where($map)->getField('value');

//         var_dump($num);
//         exit();

        $map2['cid'] = I('get.cid')?I('get.cid'):0;
        $list  = D('settings_banner')->where($map2)->limit($num)->select();
        $count = count($list);
        foreach($list as $k => &$v){
            $v['imgid']     = 'img-'.$v['id'];
            $v['filename']  = 'file-'.$v['id'];
            $v['inputname'] = 'input-'.$v['id'];
        }

        if($count == $num){
            $data['old'] = $list;
            $data['new'] = 0;
        }else if($count > $num){
            $data['old'] = array_slice($list,0,$num);
            $data['new'] = 0;
        }else{
            $data['old'] = $list;
            $num = $num - $count;
            for($i=0;$i<$num;$i++){
                $data['new'][] = array(
                    'images'=>'/Public/images/zwtp.jpg','sort'=> 255,'url'=> '','cid'=>'','alt'=>'',
                    'imgid'=>'img-n'.$i,'filename'=>'file-n'.$i,'inputname'=>'input-n'.$i,
                );
            }

        }

        $this->assign('banner',$data);

    	$this->show();
    }

		// pc端banner设置
		public function bannerPc()
		{
			$map['key'] = 'bannernum';
			$num  = D('Setting')->where($map)->getField('value');

			$list  = D('banner_pc')->limit($num)->select();
			$count = count($list);
			foreach($list as $k => &$v){
					$v['imgid']     = 'img-'.$v['id'];
					$v['filename']  = 'file-'.$v['id'];
					$v['inputname'] = 'input-'.$v['id'];
			}

			if($count == $num){
					$data['old'] = $list;
					$data['new'] = 0;
			}else if($count > $num){
					$data['old'] = array_slice($list,0,$num);
					$data['new'] = 0;
			}else{
					$data['old'] = $list;
					$num = $num - $count;
					for($i=0;$i<$num;$i++){
							$data['new'][] = array(
									'images'=>'/Public/images/zwtp.jpg','sort'=> 255,'url'=> '','cid'=>'','alt'=>'',
									'imgid'=>'img-n'.$i,'filename'=>'file-n'.$i,'inputname'=>'input-n'.$i,
							);
					}

			}

			$this->assign('banner',$data);
			$this->show();
		}

		public function doBannerPc()
		{
			$info = $_POST;
			foreach($info['bid'] as $k => $v){
					$data['images']    = $info['images'][$k];
					$data['sort']   = $info['sort'][$k];
					if($v){
							$map['id'] = $v;
							$save = D('banner_pc')->where($map)->save($data);
					}else{
							$save = D('banner_pc')->add($data);
					}
			}
			$this->success('修改成功！',U('System/bannerPc'));
		}

		// pc端banner背景设置
		public function bannerBack($value='')
		{
			$data = D('banner_back')->select();
			$product = $data[0];

			$product['filename']  = 'file-'.$product['id'];
			$product['inputname'] = 'input-'.$product['id'];

			$this->assign('product', $product);
			$this->show();
		}

		public function doBannerBack() {
        $info = I('post.');

        $map['id'] = $info['bid'];

        $data['id'] = $info['bid'];
        $data['img'] = $info['images'];
        $update = D('banner_back')->where($map)->save($data);

        if ($update) {
            $this->success('修改成功！', U('System/bannerBack'));
        } else {
            $this->error('修改失败！', U('System/bannerBack'));
        }


    }


    public function telUsBanner()
    {
        $img = $this->getImg("telUsBanner");
        $this->assign('img', $img);
        $this->show();
    }

    public function doTelUsBanner()
    {
        $info = $_POST;
        $key = "telUsBanner";

        $res = $this->saveImg($key, $info['img']);

        if ($res) {
            $this->success('修改成功！', U('System/telUsBanner'));
        } else {
            $this->error('修改失败！', U('System/telUsBanner'));
        }
    }

    public function saveImg($key, $val)
    {
        $arr = array(
            'key' => $key,
            'val' => $val
        );
        $condition = array('key' => $key);
        if (D('imgs')->where($condition)->find()) {
            $res = D('imgs')->where($condition)->save($arr);
        } else {
            $res = D('imgs')->add($arr);
        }
        return $res;
    }

    public function getImg($key)
    {
        $condition = array('key' => $key);
        $res = D('imgs')->where($condition)->find();
        $img = "";
        if ($res) {
            return $res['val'];
        }
        return $img;
    }


    public function dobanner(){
        $info = $_POST;

        foreach($info['bid'] as $k => $v){
            $data['images'] = $info['images'][$k];
            $data['url']    = $info['url'][$k];
            $data['alt']    = $info['alt'][$k];
            $data['sort']   = $info['sort'][$k];
            if($v){
                $map['id'] = $v;
                $save = D('settings_banner')->where($map)->save($data);
            }else{
                $save = D('settings_banner')->add($data);
            }
        }
        $this->success('修改成功！',U('System/banner'));
    }


    public function dosite(){

        $info = I('post.'); // 获取表单提交的信息数组
        $this->update($info,U('System/site'));     // U('System/site')表示一个url地址    /cms_pro/admin.php/System/site.html
    }


    public function doAdvantage()
    {
        $info = I('post.');
        for ($i = 0; $i < 3; $i++) {
            $map = array('id' => $i+1);
            $advTemp = array(
                'id' => $i + 1,
                'title' => $info['title' . $i],
                'img' => $info['img' . $i],
                'desc' => $info['desc' . $i]
                );
            // var_dump($advTemp);
            $adv = D("advantage")->where($map)->select();
            if ($adv) {
                $save = D("advantage")->where($map)->save($advTemp);
                echo "save success!!!!";
            } else {
                $save = D("advantage")->add($advTemp);
                echo $save;
                echo "add success!!!!";
            }
        }
        $this->success("修改成功！", U("Column/advantage"));

    }


    public function tcount(){
        $data = D('Setting')->getThrcode();
        $this->assign('data',$data);
    	$this->show();
    }

    public function dotcount(){
        $data['thr_count'] = $_POST['thr_count'];
		$data['thr_online'] = $_POST['thr_online'];
		$data['thr_other'] = $_POST['thr_other'];
        foreach($data as $k => $v){
            $map['key']     = $k;
            $value['value'] = $v;
            D('Setting')->where($map)->save($value);
        }
        $this->success('修改成功！',U('System/tcount'));
    }

    public function email(){
        $this->show();
    }

    public function doemail(){
        $info = I('post.');
        $this->update($info,U('System/email'));
    }

    public function recommend(){
        $data = D('Setting')->getset();
        $this->assign('data',$data);
        $this->show();
    }
    public function dorecommend(){
        $info = I('post.');
        $this->update($info,U('System/recommend'));
    }

    /**
     *
     * @param unknown $info 表单信息数组
     * @param unknown $url
     */
    private function update($info,$url){
        $save = D('Setting')->updatevalue($info);
        if($save){
            $this->success('修改成功！',$url);
        }else{
            $this->error('修改失败！',$url);
        }
    }


    /**
     * 展示产品页面
     */
    public function product() {

        $data = D('product')->select();

        $product = $data[0];

//         var_dump($product);
//         exit();

        $product['filename']  = 'file-'.$product['id'];
        $product['inputname'] = 'input-'.$product['id'];

        $this->assign('product', $product);

        $this->show();
    }

    /**
     * 保存产品特性
     */
    public function doproduct() {
        $info = I('post.');

//         var_dump($info);
//         exit();

        $map['id'] = $info['bid'];

        $data['id'] = $info['bid'];
        $data['img'] = $info['images'];
        $update = D('product')->where($map)->save($data);

        if ($update) {
            $this->success('修改成功！', U('System/product'));
        } else {
            $this->error('修改失败！', U('System/product'));
        }


    }



    public function doAboutUs()
    {
        $info = $_POST;

        $about = array(
            'bannerImg' => $info['bannerSrc'],
            'img' => $info['imgSrc'],
            'content' => $info['aboutUsText']
        );

        $map = array('id' => 1);
        $res = D('about_us')->where($map)->select();
        if ($res) {
            D('about_us')->where($map)->save($about);
        } else {
            D('about_us')->add($about);
        }

        $this->success('修改成功！', U('Column/aboutUs'));

    }








}
